package com.itboye.eweilisten.me.zhanghu;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.adapter.GuessLikeAdapter;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.home.ActivityExperience;
import com.itboye.eweilisten.me.personal.activity.ActivityBooks;
import com.itboye.eweilisten.model.AAABean;
import com.itboye.eweilisten.model.GuessBooks;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的账户
 */
public class ZhangHuActivity extends BaseActivity{
    //全部
    private TextView pictureTextView;
    //代付款
    private TextView movieTextView;
    //已付款
    private TextView musicTextView;

    //实现Tab滑动效果
    private ViewPager mViewPager;

    //动画图片
    private ImageView cursor;

    //动画图片偏移量
    private int offset = 0;
    private int position_one;
    private int position_two;

    //动画图片宽度
    private int bmpW;

    //当前页卡编号
    private int currIndex = 0;

    //存放Fragment
    private ArrayList<Fragment> fragmentArrayList;

    //管理Fragment
    private FragmentManager fragmentManager;

    public Context context;

    private ImageView back;//返回箭头
    private TextView tvTitle;

    MFragmentPagerAdapter mFragmentPagerAdapter;

//    private GridView guessLikeView1;
//    private GuessLikeAdapter guessLikeAdapter1;
//    private GuessBooks guessBooks1 = new GuessBooks();
//    private List<GuessBooks.ListInfo> listGuessInfo1;
//    UserPresenter presenter;
//    LinearLayout guessLike;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhang_hu);

        init();

//        presenter = new UserPresenter(this);
//        listGuessInfo1 = new ArrayList<>();
//        presenter.onGuessLike("3", "1", "16");//每页3个，第一页，16表示推荐书籍
//        guessLikeView1 = (GridView) findViewById(R.id.gv_guesslike);
//        guessLikeAdapter1 = new GuessLikeAdapter(this, listGuessInfo1);
//        guessLikeView1.setAdapter(guessLikeAdapter1);
//        guessLike=(LinearLayout)findViewById(R.id.ll_guessLike);
    }

    private void init() {
        tvTitle.setText("我的订单");
        context = this;
//        setStatusBarColor(R.color.blue);

        //初始化TextView
        InitTextView();

        //初始化ImageView
        InitImageView();

        //初始化Fragment
        InitFragment();

        //初始化ViewPager
        InitViewPager();
    }

    /**
     * 初始化头标
     */
    private void InitTextView() {
        //全部头标
        pictureTextView = (TextView) findViewById(R.id.picture_text);
        //待付款头标
        movieTextView = (TextView) findViewById(R.id.movie_text);
        //已付款头标
        musicTextView = (TextView) findViewById(R.id.music_text);

        //添加点击事件
        pictureTextView.setOnClickListener(new MyOnClickListener(0));
        movieTextView.setOnClickListener(new MyOnClickListener(1));
        musicTextView.setOnClickListener(new MyOnClickListener(2));
    }

    /**
     * 初始化页卡内容区
     */
    private void InitViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.vPager);
        mFragmentPagerAdapter=new MFragmentPagerAdapter(fragmentManager, fragmentArrayList);
        mViewPager.setAdapter(mFragmentPagerAdapter);

        //让ViewPager缓存2个页面
        mViewPager.setOffscreenPageLimit(2);

        //设置默认打开第一页
        mViewPager.setCurrentItem(0);

        //将顶部文字恢复默认值
        resetTextViewTextColor();
        pictureTextView.setTextColor(getResources().getColor(R.color.main_color));

        //设置viewpager页面滑动监听事件
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * 初始化动画
     */
    private void InitImageView() {
        cursor = (ImageView) findViewById(R.id.cursor);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        // 获取分辨率宽度
        int screenW = dm.widthPixels;

        bmpW = (screenW / 3);

        //设置动画图片宽度
        setBmpW(cursor, bmpW);
        offset = 0;

        //动画图片偏移量赋值
        position_one = (int) (screenW / 3.0);
        position_two = position_one * 2;

    }

    /**
     * 初始化Fragment，并添加到ArrayList中
     */
    private void InitFragment() {
        fragmentArrayList = new ArrayList<Fragment>();
        fragmentArrayList.add(new ZhangHuFragment(0));
        fragmentArrayList.add(new ZhangHuFragment(1));
        fragmentArrayList.add(new ZhangHuFragment(90));
        fragmentManager = getSupportFragmentManager();

    }

    /**
     * 头标点击监听
     *
     * @author weizhi
     * @version 1.0
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    /**
     * 页卡切换监听
     *
     * @author weizhi
     * @version 1.0
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int position) {
            Animation animation = null;
            switch (position) {

                //当前为页卡1
                case 0:
                    //从页卡1跳转转到页卡2
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, 0, 0, 0);
                        resetTextViewTextColor();
                        pictureTextView.setTextColor(getResources().getColor(R.color.main_color));
                    } else if (currIndex == 2) {//从页卡1跳转转到页卡3
                        animation = new TranslateAnimation(position_two, 0, 0, 0);
                        resetTextViewTextColor();
                        pictureTextView.setTextColor(getResources().getColor(R.color.main_color));
                    }
                    break;

                //当前为页卡2
                case 1:
                    //从页卡1跳转转到页卡2
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_one, 0, 0);
                        resetTextViewTextColor();
                        movieTextView.setTextColor(getResources().getColor(R.color.main_color));
                    } else if (currIndex == 2) { //从页卡1跳转转到页卡2
                        animation = new TranslateAnimation(position_two, position_one, 0, 0);
                        resetTextViewTextColor();
                        movieTextView.setTextColor(getResources().getColor(R.color.main_color));
                    }
                    break;

                //当前为页卡3
                case 2:
                    //从页卡1跳转转到页卡2
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_two, 0, 0);
                        resetTextViewTextColor();
                        musicTextView.setTextColor(getResources().getColor(R.color.main_color));
                    } else if (currIndex == 1) {//从页卡1跳转转到页卡2
                        animation = new TranslateAnimation(position_one, position_two, 0, 0);
                        resetTextViewTextColor();
                        musicTextView.setTextColor(getResources().getColor(R.color.main_color));
                    }
                    break;
            }
            currIndex = position;

            animation.setFillAfter(true);// true:图片停在动画结束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
    /**
     * 设置动画图片宽度
     *
     * @param mWidth
     */
    private void setBmpW(ImageView imageView, int mWidth) {
        ViewGroup.LayoutParams para;
        para = imageView.getLayoutParams();
        para.width = mWidth;
        imageView.setLayoutParams(para);
    }

    /**
     * 将顶部文字恢复默认值
     */
    private void resetTextViewTextColor() {

        pictureTextView.setTextColor(getResources().getColor(R.color.rgb_434343));
        movieTextView.setTextColor(getResources().getColor(R.color.rgb_434343));
        musicTextView.setTextColor(getResources().getColor(R.color.rgb_434343));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

        }
    }

    public class MFragmentPagerAdapter extends FragmentPagerAdapter {

        //存放Fragment的数组
        private ArrayList<Fragment> fragmentsList;

        public MFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragmentsList) {
            super(fm);
            this.fragmentsList = fragmentsList;
        }

        @Override
        public Fragment getItem(int position) {

            return fragmentsList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentsList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }
    }
}
